package com.xkdx.guangguang.fragment.user;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.HashMap;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.open.HttpStatusException;
import com.tencent.open.NetworkUnavailableException;
import com.tencent.tauth.Constants;
import com.tencent.tauth.IRequestListener;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.UsersAPI;
import com.weibo.sdk.android.keep.AccessTokenKeeper;
import com.weibo.sdk.android.net.RequestListener;
import com.weibo.sdk.android.sso.SsoHandler;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.UserActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.user.UserRegistModule.User;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;

public class UserLoginFragment extends BaseFragment implements OnClickListener {
	private static final String SCOPE = "all";
	private View view;
	private Button user_regist, login_back, login, reset_password,
			quick_regist;
	private ImageView user_sina, user_tecent;
	private TextView login_password, login_name;
	private UserLoginAction loginAction;
	private UserLoginModule loginModule;
	private UserLoginPresenter loginPresenter;
	private User user;
	private Tencent mTencent;
	private Weibo sinaWeibo;
	private String QQOpenID, QQExpireTime, QQToken; // 腾讯登陆的相关变量
	private String UserNickName; // 用户昵称
	private String UserSex = "-1"; // 用户性别
	private String UserMobilePhone = ""; // 用户手机
	private String UserEmail = "";// 用户邮箱
	private String DeviceSN;// 设备串号
	private String DeviceModel;// 设备型号
	private String OSVersion;// 操作系统版本
	private String MobilePhone;
	private String UserBirthday = "";// 用户生日
	// private String picUrl;// 用户头像路径
	private LinearLayout ll;
	private RelativeLayout rl_login;
	private ByteArrayOutputStream stream = null;


	private SsoHandler mSsoHandler;
	private String sinaUid;
	private String sinaWeiboToken;
	private String sinaWeiboExpireTime;
	public long sinaWeiboUserID; // 新浪微博用户id
	public String PictureUrl;// 用户头像路径
	public static Oauth2AccessToken AccessToken; // 新浪微博token

	private UserLoginSinaAction loginSinaAction;
	private UserLoginSinaPresenter loginsinaPresenter;
	private UserLoginTencentAction loginTecentAction;
	private UserLoginTencentModule loginTecentModule;
	private UserLoginTencentPresenter loginTecentPresenter;

	/**
	 * 快速注册
	 */

	private UserQuickRegistAction logquickAction;
	private UserQuickRegistModule logquickModule;
	private UserQuickRegistPresenter logquickPresenter;
	private com.xkdx.guangguang.fragment.user.UserQuickRegistModule.User quickUser;

	private UserSharePrefence user_sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		user_sp = new UserSharePrefence(getActivity(), IConstants.SP_USER);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.user_login, container, false);

		findViews();
		return view;
	}

	// 初始化
	private void findViews() {
		user_regist = (Button) view.findViewById(R.id.user_regist_button);
		login_back = (Button) view.findViewById(R.id.login_back);
		login = (Button) view.findViewById(R.id.user_login_button);
		quick_regist = (Button) view.findViewById(R.id.user_quickregist_button);
		login_name = (TextView) view.findViewById(R.id.login_name);
		login_password = (TextView) view.findViewById(R.id.login_password);
		reset_password = (Button) view.findViewById(R.id.reset_password);
		user_sina = (ImageView) view.findViewById(R.id.user_sina);
		user_tecent = (ImageView) view.findViewById(R.id.user_tencent);
		rl_login = (RelativeLayout) view.findViewById(R.id.rl_login);
		login_back.setOnClickListener(this);
		user_regist.setOnClickListener(this);
		reset_password.setOnClickListener(this);
		user_sina.setOnClickListener(this);
		user_tecent.setOnClickListener(this);
		login.setOnClickListener(this);
		quick_regist.setOnClickListener(this);

		// 获取手机号
		TelephonyManager mTelephonyManager = (TelephonyManager) getActivity()
				.getSystemService(getActivity().TELEPHONY_SERVICE);
		MobilePhone = mTelephonyManager.getLine1Number();
		// 获取设备串号
		DeviceSN = mTelephonyManager.getDeviceId();
		// 获取设备型号
		DeviceModel = Build.MODEL;
		// 获取系统版本
		OSVersion = Build.VERSION.SDK;
		SinaWeiBoLogin(); // 新浪微博登录
		TencentLogin();// 腾讯qq登录
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.user_login_button: // 登陆按钮

			IConstants.LoginWhich = 1;
			IConstants.IsDisEnable = false;
			/**
			 * 弹出键盘
			 */
			InputMethodManager im = (InputMethodManager) getActivity()
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			if (im.isActive()) {
				im.hideSoftInputFromWindow(login_name.getWindowToken(), 0);
			}

			if (!checkTextView()) {
				return;
			}

			showLoading();
			break;

		case R.id.reset_password: // 重置密码
			setBtnUnClick();
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			UserResetPwdFragment userRsePwdFragment = new UserResetPwdFragment();
			ft.replace(R.id.fragment, userRsePwdFragment, "userRsePwdFragment");
			ft.addToBackStack(null);
			ft.commit();
			break;

		case R.id.user_regist_button: // 注册按钮
			setBtnUnClick();
			FragmentTransaction ft_regist = getFragmentManager()
					.beginTransaction();
			UserRegisteFragment registeFragment = new UserRegisteFragment();
			ft_regist
					.replace(R.id.fragment, registeFragment, "registeFragment");
			ft_regist.addToBackStack(null);
			ft_regist.commit();
			break;

		case R.id.user_sina: // 新浪微博登陆
			IConstants.LoginWhich = 2;
			IConstants.IsDisEnable = false;
			//setBtnUnClick();
			loginsinaPresenter = new UserLoginSinaPresenter(this);
			dataLoadDialog.show();
			dataLoadDialog
			.setOnCancelListener(new DialogInterface.OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					loginsinaPresenter.cancel(true);
					user_sp.clear();
					if (getFragmentManager() != null) {
						getFragmentManager().popBackStack();
					}
				}
			});
			
			getAlertSinaLogin();
			break;
		case R.id.user_tencent: // 腾讯qq登陆
			IConstants.LoginWhich = 3;
			IConstants.IsDisEnable = false;
			loginTecentPresenter = new UserLoginTencentPresenter(this);
			getAlertTencentLogin();
			break;
		case R.id.user_quickregist_button: // 一键注册
			IConstants.LoginWhich = 7;
			IConstants.IsDisEnable = true;
			logquickAction = new UserQuickRegistAction(DeviceSN, DeviceModel,
					OSVersion, MobilePhone);
			logquickModule = new UserQuickRegistModule(getActivity());
			logquickPresenter = new UserQuickRegistPresenter(this);
			((ManagerActivity) getActivity()).addCurrentTask(logquickPresenter);
			logquickPresenter.setActitons(logquickAction);
			logquickPresenter.setModule(logquickModule);
			logquickPresenter.execute();
			dataLoadDialog
					.setOnCancelListener(new DialogInterface.OnCancelListener() {
						@Override
						public void onCancel(DialogInterface dialog) {
							logquickPresenter.cancel(true);
							user_sp.clear();
							if (getFragmentManager() != null) {
								getFragmentManager().popBackStack();
							}
						}
					});
			dataLoadDialog.show();
			break;
		case R.id.login_back:
			FragmentManager fm = getFragmentManager();
			fm.popBackStack();
			break;
		}
	}

	/**
	 * 设置按钮不能点击
	 */

	private void setBtnUnClick() {
		user_regist.setClickable(false);
		reset_password.setClickable(false);
		user_sina.setClickable(false);
		user_tecent.setClickable(false);
		login.setClickable(false);
		quick_regist.setClickable(false);
	}

	/**
	 * 设置按钮可以点击
	 */
	private void setBtnClickable() {
		user_regist.setClickable(true);
		reset_password.setClickable(true);
		user_sina.setClickable(true);
		user_tecent.setClickable(true);
		login.setClickable(true);
		quick_regist.setClickable(true);
	}

	/**
	 * 页面不可见时重新设置按钮可点击
	 */
	@Override
	public void onPause() {
		setBtnClickable();
		super.onPause();
	}

	/**
	 * 
	 * login callback
	 * 
	 * @author zhangshun
	 * 
	 */
	class AuthDialogListener implements WeiboAuthListener {

		@Override
		public void onComplete(Bundle values) {

			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			sinaUid = values.getString("uid");
			sinaWeiboToken = token; // 新浪微博token
			sinaWeiboExpireTime = expires_in; // 微博登录有效日期
			UserLoginFragment.AccessToken = new Oauth2AccessToken(token,
					expires_in);

			if (UserLoginFragment.AccessToken.isSessionValid()) {
				try {
					Class sso = Class
							.forName("com.weibo.sdk.android.api.WeiboAPI");// 如果支持weiboapi的话，显示api功能演示入口按钮
				} catch (ClassNotFoundException e) {
				}
				AccessTokenKeeper.keepAccessToken(getActivity(),
						UserLoginFragment.AccessToken);
				
				if (Long.parseLong(sinaUid) > 0) {

					sinaWeiboUserID = Long.parseLong(sinaUid);
					UsersAPI user = new UsersAPI(AccessToken);
					user.show(sinaWeiboUserID, new RequestListener() { // 注意：内部是线程处理
								@Override
								public void onIOException(IOException e) {
								}

								@Override
								public void onError(WeiboException e) {
								}

								@Override
								public void onComplete(String response) {

									JSONObject userObject;
									try {
										userObject = new JSONObject(response);
										UserNickName = userObject
												.getString("screen_name");
										UserSex = (String) userObject
												.getString("gender");
										if ("m".equals(UserSex)) {
											UserSex = "0";
										} else if ("f".equals(UserSex)) {
											UserSex = "1";
										} else {
											UserSex = "-1";
										}

										PictureUrl = userObject
												.getString("profile_image_url");
										// saveSinaHeader(); // 上传新浪微博头像
										Log.i("url",PictureUrl);
										h.sendEmptyMessage(2);

									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});

					// loginSinaAction = new UserLoginSinaAction(
					// String.valueOf(sinaWeiboUserID), sinaWeiboToken,
					// sinaWeiboExpireTime, UserNickName, UserSex,
					// UserBirthday, UserEmail, UserMobilePhone, DeviceSN,
					// DeviceModel, OSVersion);
					// loginTecentModule = new UserLoginTencentModule(
					// getActivity());
					//
					// ((ManagerActivity) getActivity())
					// .addCurrentTask(loginsinaPresenter);
					//
					// SaveHeadImageAsy saveHeadImageAsy = new
					// SaveHeadImageAsy();
					// saveHeadImageAsy.execute();
					//
					//
					// loginsinaPresenter.setActitons(loginSinaAction);
					// loginsinaPresenter.setModule(loginTecentModule);
					// loginsinaPresenter.execute();
					// dataLoadDialog
					// .setOnCancelListener(new
					// DialogInterface.OnCancelListener() {
					// @Override
					// public void onCancel(DialogInterface dialog) {
					// loginsinaPresenter.cancel(true);
					// user_sp.clear();
					// if (getFragmentManager() != null) {
					// getFragmentManager().popBackStack();
					// }
					// }
					// });

				}
			}
		}

		@Override
		public void onError(WeiboDialogError e) {
		}

		@Override
		public void onCancel() {

		}

		@Override
		public void onWeiboException(WeiboException e) {

		}

	}

	// 新浪微博登录
	private void getAlertSinaLogin() {

		try {
			Class sso = Class.forName("com.weibo.sdk.android.sso.SsoHandler");
			mSsoHandler = new SsoHandler(getActivity(), sinaWeibo);
			mSsoHandler.authorize(new AuthDialogListener());

		} catch (ClassNotFoundException e) {
			// Log.i("ok","没有进入sso");
			sinaWeibo.authorize(getActivity(), new AuthDialogListener());
		}
	}

	public SsoHandler GetSsoHandler() {
		return mSsoHandler;
	}

	/**
	 * 腾讯qq方式登陆
	 */
	private void getAlertTencentLogin() {
		if (!mTencent.isSessionValid()) {
			IUiListener listener = new BaseUiListener() {

				@Override
				protected void doComplete(JSONObject values) {
					if (isValid()) {
						try {
							QQExpireTime = values.getString("expires_in");
						} catch (JSONException e) {
							e.printStackTrace();
						}

						// StartLoading();
						dataLoadDialog.show();
						dataLoadDialog
						.setOnCancelListener(new DialogInterface.OnCancelListener() {
							@Override
							public void onCancel(DialogInterface dialog) {
								loginTecentPresenter.cancel(true);
								user_sp.clear();

								if (getFragmentManager() != null) {

									getFragmentManager().popBackStack();

								}

							}
						});
						mTencent.requestAsync(Constants.GRAPH_SIMPLE_USER_INFO,
								null, Constants.HTTP_GET,
								new BaseApiListener(), null);
					}
				}
			};

			mTencent.login(getActivity(), SCOPE, listener);
			// }
		} else {
			mTencent.logout(getActivity());
		}
	}

	/**
	 * tencent 的监听认证接口
	 * 
	 * @author Administrator
	 * 
	 */
	private class BaseApiListener implements IRequestListener {

		@Override
		public void onComplete(final JSONObject response, Object state) {
			showResult(response.toString());
			doComplete(response, state);
		}

		protected void doComplete(JSONObject response, Object state) {

		}

		@Override
		public void onIOException(final IOException e, Object state) {

		}

		@Override
		public void onMalformedURLException(final MalformedURLException e,
				Object state) {

		}

		@Override
		public void onJSONException(final JSONException e, Object state) {

		}

		@Override
		public void onConnectTimeoutException(ConnectTimeoutException arg0,
				Object arg1) {

		}

		@Override
		public void onSocketTimeoutException(SocketTimeoutException arg0,
				Object arg1) {

		}

		@Override
		public void onUnknowException(Exception arg0, Object arg1) {

		}

		@Override
		public void onHttpStatusException(HttpStatusException arg0, Object arg1) {

		}

		@Override
		public void onNetworkUnavailableException(
				NetworkUnavailableException arg0, Object arg1) {

		}
	}

	private void showResult(final String msg) {

		// new AsyncTask<String, Void, String>() {
		//
		// @Override
		// protected String doInBackground(String... params) {

		JSONObject userinfoObject;
		try {
			userinfoObject = new JSONObject(msg);

			UserNickName = userinfoObject.getString("nickname");
			PictureUrl = userinfoObject.getString("figureurl_1");
			UserSex = (String) userinfoObject.getString("gender");
			if ("男".equals(UserSex)) {
				UserSex = "0";
			} else if ("女".equals(UserSex)) {
				UserSex = "1";
			} else {
				UserSex = "-1";
			}

			// saveTencentHeader(); // 保存腾讯qq头像
			// h.sendEmptyMessage(1);
			loginTecentAction = new UserLoginTencentAction(
					String.valueOf(QQOpenID), QQToken, QQExpireTime,
					UserNickName, UserSex, UserBirthday, UserEmail,
					UserMobilePhone, DeviceSN, DeviceModel, OSVersion);
			loginTecentModule = new UserLoginTencentModule(getActivity());

			((ManagerActivity) getActivity())
					.addCurrentTask(loginTecentPresenter);
			SaveHeadImageAsy saveHeadImageAsy = new SaveHeadImageAsy();
			saveHeadImageAsy.execute();
			loginTecentPresenter.setActitons(loginTecentAction);
			loginTecentPresenter.setModule(loginTecentModule);
			loginTecentPresenter.execute();
//			dataLoadDialog
//					.setOnCancelListener(new DialogInterface.OnCancelListener() {
//						@Override
//						public void onCancel(DialogInterface dialog) {
//							loginTecentPresenter.cancel(true);
//							user_sp.clear();
//
//							if (getFragmentManager() != null) {
//
//								getFragmentManager().popBackStack();
//
//							}
//
//						}
//					});
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// /**
	// * 第三方组件登陆会回闪，增加进度框提高用户体验
	// */
	// private void StartLoading() {
	//
	// ll = new LinearLayout(getActivity());
	// ll.setLayoutParams(new LayoutParams(
	// LinearLayout.LayoutParams.FILL_PARENT,
	// LinearLayout.LayoutParams.FILL_PARENT));
	// ll.setOrientation(LinearLayout.VERTICAL);
	// ll.setBackgroundColor(getResources().getColor(R.color.transparent));
	//
	// LinearLayout ll1 = new LinearLayout(getActivity());
	// ll1.setLayoutParams(new LayoutParams(
	// LinearLayout.LayoutParams.WRAP_CONTENT,
	// LinearLayout.LayoutParams.WRAP_CONTENT));
	// ll1.setBackgroundColor(getResources().getColor(R.color.black));
	// ll.setOrientation(LinearLayout.HORIZONTAL);
	// // 添加进展条
	// ProgressBar progressBar = new ProgressBar(getActivity());
	// TextView tv = new TextView(getActivity());
	// tv.setText("逛逛正在进行身份认证,请稍等...");
	// tv.setGravity(Gravity.CENTER_VERTICAL);
	// tv.setTextSize(14.0f);
	// tv.setTextColor(getResources().getColor(R.color.white));
	//
	// ll1.addView(progressBar, new LinearLayout.LayoutParams(60, 60));
	// ll1.addView(tv, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
	// LinearLayout.LayoutParams.FILL_PARENT));
	//
	// ll.setGravity(Gravity.CENTER);
	// ll.addView(ll1);
	//
	// rl_login.addView(ll);
	//
	// setUpComponent();
	//
	// }
	//
	// /**
	// * 等待过程中其他组件不可用
	// */
	// private void setUpComponent() {
	//
	// login.setClickable(false);
	// user_regist.setClickable(false);
	// user_sina.setClickable(false);
	// login_name.setFocusable(false);
	// login_password.setFocusable(false);
	// }

	/**
	 * 根据是否有openid和session可用判断是否获取用户信息
	 * 
	 * @return
	 */
	private boolean isValid() {
		boolean isValid = (mTencent.isSessionValid())
				&& (mTencent.getOpenId() != null);
		return isValid;

	}

	// 腾讯qq登录时的事件监听
	private class BaseUiListener implements IUiListener {

		@Override
		public void onComplete(JSONObject response) {
			QQOpenID = mTencent.getOpenId();
			QQToken = mTencent.getAccessToken();
			doComplete(response);
		}

		protected void doComplete(JSONObject values) {
		}

		@Override
		public void onError(UiError e) {

		}

		@Override
		public void onCancel() {
		}
	}

	/**
	 * 检查TextView是否为空
	 */
	private boolean checkTextView() {
		if (TextUtils.isEmpty(login_name.getText().toString())) {
			Toast.makeText(getActivity(), "用户名不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(login_password.getText().toString())) {
			Toast.makeText(getActivity(), "密码不能为空", Toast.LENGTH_SHORT).show();
			return false;
		}

		return true;
	}

	/**
	 * 普通登陆成功
	 * 
	 * @param msg
	 */
	public void showLoaded(HashMap<String, AbsModule> msg) {
		if (dataLoadDialog != null && dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}

		if (msg.containsKey("0")) {
			user = ((UserLoginModule) msg.get("0")).user;
			if (user != null && user.getStatus() != null
					&& user.getStatus().equals("Success")) {
				// 保存头像
				if (user_sp != null && (!"".equals(user_sp.getLoginToken()))) {
					EnterLoginSuccessFragment();
				}
			} else {
				Toast.makeText(getActivity(), user.getMessage(),
						Toast.LENGTH_SHORT).show();
			}
		} else {
			showErrorTip(msg);
		}

	}

	public void showTencentLoaded(HashMap<String, AbsModule> msg) {
		if (dataLoadDialog != null && dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}

		if (msg.containsKey("0")) {
			user = ((UserLoginTencentModule) msg.get("0")).user;
			if (user != null && user.getStatus() != null
					&& user.getStatus().equals("Success")) {
				
				if (user_sp != null && (!"".equals(user_sp.getLoginToken()))) {
					EnterLoginSuccessFragment();
				}
			} else {
				Toast.makeText(getActivity(), user.getMessage(),
						Toast.LENGTH_SHORT).show();
			}
		} else {
			showErrorTip(msg);
		}

	}

	/**
	 * 跳转到登陆成功页面
	 */
	private void EnterLoginSuccessFragment() {
		getFragmentManager().popBackStack();
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		UserLoginSuccessFragment userLoginSuccessFragment = new UserLoginSuccessFragment();

		ft.replace(R.id.fragment, userLoginSuccessFragment,
				"userLoginSuccessFragment");
		ft.addToBackStack(null);
		ft.commit();
	}

	/**
	 * 初始化腾讯实例
	 */
	private void TencentLogin() {
		mTencent = ((UserActivity) getActivity()).getTencent();
	}

	/**
	 * 初始化新浪微博
	 * 
	 * @return
	 */
	public Weibo SinaWeiBoLogin() {
		sinaWeibo = Weibo.getInstance(IConstants.CONSUMER_KEY,
				IConstants.REDIRECT_URL);
		return sinaWeibo;
	}

	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (getActivity() == null) {
				return;
			}

			if (msg.what == 2) {
				loginSinaAction = new UserLoginSinaAction(
						String.valueOf(sinaWeiboUserID), sinaWeiboToken,
						sinaWeiboExpireTime, UserNickName, UserSex,
						UserBirthday, UserEmail, UserMobilePhone, DeviceSN,
						DeviceModel, OSVersion);
				loginTecentModule = new UserLoginTencentModule(getActivity());

				((ManagerActivity) getActivity())
						.addCurrentTask(loginsinaPresenter);

				SaveHeadImageAsy saveHeadImageAsy = new SaveHeadImageAsy();
				saveHeadImageAsy.execute();

				loginsinaPresenter.setActitons(loginSinaAction);
				loginsinaPresenter.setModule(loginTecentModule);
				loginsinaPresenter.execute();
//				dataLoadDialog
//						.setOnCancelListener(new DialogInterface.OnCancelListener() {
//							@Override
//							public void onCancel(DialogInterface dialog) {
//								loginsinaPresenter.cancel(true);
//								user_sp.clear();
//								if (getFragmentManager() != null) {
//									getFragmentManager().popBackStack();
//								}
//							}
//						});
			}

		}
	};

	@Override
	public void showClick(HashMap<String, AbsModule> msg) {
		if (dataLoadDialog != null && dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}

		if (msg.containsKey("0")) {
			quickUser = ((UserQuickRegistModule) msg.get("0")).user;
			if (quickUser != null && quickUser.getStatus() != null
					&& quickUser.getStatus().equals("Success")) {
				if (user_sp != null && (!"".equals(user_sp.getLoginToken()))) {
					EnterLoginSuccessFragment();
				}
			} else {
				Toast.makeText(getActivity(), quickUser.getMessage(),
						Toast.LENGTH_SHORT).show();
			}
		} else {
			showErrorTip(msg);
		}

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {

	}

	/**
	 * 登陆按钮操作
	 */
	@Override
	public void showLoading() {

		loginAction = new UserLoginAction(getActivity(), login_name.getText()
				.toString().trim(), login_password.getText().toString().trim());
		loginModule = new UserLoginModule(getActivity());
		loginPresenter = new UserLoginPresenter(this);
		((ManagerActivity) getActivity()).addCurrentTask(loginPresenter);
		loginPresenter.setActitons(loginAction);
		loginPresenter.setModule(loginModule);
		loginPresenter.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (loginPresenter != null) {
					loginPresenter.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
		dataLoadDialog.show();

	}

	@Override
	public void findView() {
	}

	@Override
	public void setOnClick() {
	}

	class SaveHeadImageAsy extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {

			if (android.os.Environment.getExternalStorageState().equals(
					android.os.Environment.MEDIA_MOUNTED)) {
				File dirFile = new File(Environment
						.getExternalStorageDirectory().getAbsolutePath()
						+ "/GuangGuang/user/");
				if (!dirFile.exists()) {
					dirFile.mkdirs();
				}

				String headUrl = PictureUrl;
				

				try {
					Bitmap photo = BitmapFactory.decodeStream(new URL(headUrl)
							.openStream());
					CompressFormat format = Bitmap.CompressFormat.JPEG;
					int quality = 100;
					OutputStream stream = null;

					stream = new FileOutputStream(Environment
							.getExternalStorageDirectory().getAbsolutePath()
							+ "/GuangGuang/user/head.jpg");

					photo.compress(format, quality, stream);
				} catch (Exception e) {
					return null;
				}

			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}
	}

}
